﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using Hi_Baidu.Timer;
using Hi_Baidu.Entry.CollectionObject;
using System.Collections;
using System.Text;
using Hi_Baidu.ProcessModel;

namespace Hi_Baidu
{
	public partial class Win_PostAddFriend : IMetaData, IPopWindowAction
	{
		public Win_PostAddFriend()
		{
			this.InitializeComponent();
			
			// 在此点之下插入创建对象所需的代码。
		}

        public Win_PostAddFriend(String Key)
        {
            this.InitializeComponent();
            this.key = Key;
            foreach (DictionaryEntry item in Globle.itemCollection.Members)
            {
                ComboBoxItem menuItem = new ComboBoxItem();
                menuItem.Content = (item.Value as Item).Name;
                menuItem.Tag = item.Key;
                this.cbx_Item.Items.Add(menuItem);
            }
            this.cbx_Item.SelectedIndex = 0;
        }

        private void Window_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.ChangedButton == System.Windows.Input.MouseButton.Left)
            {
                this.DragMove();
            }
        }

        #region IMetaData 成员

        private String key;
        public string Key
        {
            get
            {
                return key;
            }
            set
            {
                key = value;
            }
        }

        #endregion

        #region IPopWindowAction 成员

        public void DisposeToManager()
        {
            IMetaData win = null;
           if((win = Globle.popWindowManager.GetMemberByKey(this.key)) != null)
           {
               Globle.popWindowManager.Remove(win);
           }
        }

        #endregion

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.DisposeToManager();
        }

        private void btn_Cancle_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Img_GetFriendInfo_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            
        }

        private void btn_OK_Click(object sender, RoutedEventArgs e)
        {
            //查找
            if (this.btn_OK.Content.ToString() == "查找")
            {
                String Number = this.txt_Number.Text.Trim().Replace(" ", "");
                if (string.IsNullOrEmpty(Number) || Number == Encoding.Default.GetString(Globle.user.DIC_USERINFO["USERNAME"]) || Globle.friendCollection.GetMemberByKey(Number) != null)
                {
                    Win_Notify win_Notify = new Win_Notify();
                    win_Notify.tb_Notify.Text = "您输入的好友号码不正确 ?!";
                    win_Notify.ShowDialog();
                }
                else
                {
                     //向服务器发送获取指定好友信息请求
                    FriendProcessModel.GetPostFriendInfo(Encoding.Default.GetBytes(Number), Globle.user.DIC_USERINFO["USERNAME"]);
                    //创建请求超时器
                    GetPostFriendInfoTimeOutEvent getPostFriendInfoTimeOutEvent = new GetPostFriendInfoTimeOutEvent(Number, "GetPostFriendInfo" + Number, 10000, 3);
                    //打入超时器集合 
                    Globle.timeoutTimerManager.InstallTimer(getPostFriendInfoTimeOutEvent);
                }
            }
            //添加
            else
            {
                String Number = this.txt_Number.Text.Trim();
                FriendProcessModel.AddFriend(Encoding.Default.GetBytes(Number), Globle.user.DIC_USERINFO["USERNAME"], Encoding.Default.GetBytes((this.cbx_Item.SelectedItem as ComboBoxItem).Tag.ToString()));
                this.Close();
            }
        }
    }
}